class smx_apollo {
  require smx_os

  Exec {
    path      => [
      '/usr/bin',
      '/bin',
    ],
    logoutput => true,
    timeout   => 0,
  }

  # This one is a deal breaker, taking forever for non-China users. Logic now moved to pre_prevision.sh
  #  exec { 'get-apollo':
  #    creates => "/usr/local/apache-apollo-1.7.1/readme.html", # We will not fetch and extract if it has been extracted. We take readme file as a sign.
  #    cwd => "/tmp",
  #    #  Wow, always download from China?!!
  #    # command => "wget -c -t 0 http://apache.dataguru.cn/activemq/activemq-apollo/1.7.1/apache-apollo-1.7.1-unix-distro.tar.gz && cd /usr/local && tar xzvf /tmp/apache-apollo-1.7.1-unix-distro.tar.gz",
  #
  #    before => Exec['create-default'],
  #  }

  exec { 'create-default':
    creates => '/var/lib/default/etc',
    cwd     => "/var/lib",
    command => "/usr/local/apache-apollo-1.7.1/bin/apollo create default",
    before  => [ File['groups.properties'], File['users.properties'], File['apollo.xml'] ]
  }

  file { 'groups.properties':
    path   => "/var/lib/default/etc/groups.properties",
    ensure => file,
    mode   => 644, group  => root, owner  => root,
    source => "puppet:///modules/smx_apollo/groups.properties",
  }

  file { 'users.properties':
    path   => "/var/lib/default/etc/users.properties",
    ensure => file,
    mode   => 644, group  => root, owner  => root,
    source => "puppet:///modules/smx_apollo/users.properties",
  }

  file { 'apollo.xml':
    path    => "/var/lib/default/etc/apollo.xml",
    ensure  => file,
    mode    => 644, group  => root, owner  => root,
    content => template('smx_apollo/apollo.xml.erb'),
  }

  file { '/etc/init.d/apollo':
    ensure => file,
    mode   => 755, group  => root, owner  => root,
    source => "puppet:///modules/smx_apollo/apollo",
  }

  service { 'apollo':
    ensure    => running,
    enable    => true,
    subscribe => [ File['groups.properties'], File['users.properties'], File['apollo.xml'], File['/etc/init.d/apollo'] ]
  }
}
